package com.itcm.tcpdemo.service;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

@Service
public class HttpStreamService {

    private final WebClient webClient;

    // app-glEPN0rNW6l0uVBo76SbcF4z
    public HttpStreamService(WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder
                .baseUrl("http://localhost/v1/chat-messages")
                .defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer app-glEPN0rNW6l0uVBo76SbcF4z")
                .build();
    }

    public Flux<String> streamChatMessages(String query, String conversationId, String deviceId) {
        return webClient.post()
                .contentType(MediaType.APPLICATION_JSON)
                .bodyValue("{\n" +
                        "                        \"inputs\": {},\n" +
                        "                        \"query\": \"%s\",\n" +
                        "                        \"response_mode\": \"streaming\",\n" +
                        "                        \"conversation_id\": \"%s\",\n" +
                        "                        \"user\": \"%s\",\n" +
                        "                        \"files\": []\n" +
                        "                    }".format(query, conversationId, deviceId))
                .retrieve()
                .bodyToFlux(String.class);
    }
}
